{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Scalable GP Classification in 1D (w/ KISS-GP)\n",
    "\n",
    "This example shows how to use grid interpolation based variational classification with an `AbstractVariationalGP` using a `GridInterpolationVariationalStrategy` module. This classification module is designed for when the inputs of the function you're modeling are one-dimensional.\n",
    "\n",
    "The use of inducing points allows for scaling up the training data by making computational complexity linear instead of cubic.\n",
    "\n",
    "In this example, we’re modeling a function that is periodically labeled cycling every 1/8 (think of a square wave with period 1/4)\n",
    "\n",
    "This notebook doesn't use cuda, in general we recommend GPU use if possible and most of our notebooks utilize cuda as well.\n",
    "\n",
    "Kernel interpolation for scalable structured Gaussian processes (KISS-GP) was introduced in this paper:\n",
    "http://proceedings.mlr.press/v37/wilson15.pdf\n",
    "\n",
    "KISS-GP with SVI for classification was introduced in this paper:\n",
    "https://papers.nips.cc/paper/6426-stochastic-variational-deep-kernel-learning.pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import torch\n",
    "import gpytorch\n",
    "from matplotlib import pyplot as plt\n",
    "from math import exp\n",
    "\n",
    "%matplotlib inline\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = torch.linspace(0, 1, 26)\n",
    "train_y = torch.sign(torch.cos(train_x * (2 * math.pi))).add(1).div(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpytorch.models import AbstractVariationalGP\n",
    "from gpytorch.variational import CholeskyVariationalDistribution\n",
    "from gpytorch.variational import GridInterpolationVariationalStrategy\n",
    "\n",
    "\n",
    "class GPClassificationModel(AbstractVariationalGP):\n",
    "    def __init__(self, grid_size=128, grid_bounds=[(0, 1)]):\n",
    "        variational_distribution = CholeskyVariationalDistribution(grid_size)\n",
    "        variational_strategy = GridInterpolationVariationalStrategy(self, grid_size, grid_bounds, variational_distribution)\n",
    "        super(GPClassificationModel, self).__init__(variational_strategy)\n",
    "        self.mean_module = gpytorch.means.ConstantMean()\n",
    "        self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())\n",
    "        \n",
    "    def forward(self,x):\n",
    "        mean_x = self.mean_module(x)\n",
    "        covar_x = self.covar_module(x)\n",
    "        latent_pred = gpytorch.distributions.MultivariateNormal(mean_x, covar_x)\n",
    "        return latent_pred\n",
    "\n",
    "\n",
    "model = GPClassificationModel()\n",
    "likelihood = gpytorch.likelihoods.BernoulliLikelihood()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter 1/100 - Loss: 0.070\n",
      "Iter 2/100 - Loss: 14.834\n",
      "Iter 3/100 - Loss: 0.977\n",
      "Iter 4/100 - Loss: 3.547\n",
      "Iter 5/100 - Loss: 8.699\n",
      "Iter 6/100 - Loss: 6.352\n",
      "Iter 7/100 - Loss: 1.795\n",
      "Iter 8/100 - Loss: 0.188\n",
      "Iter 9/100 - Loss: 2.075\n",
      "Iter 10/100 - Loss: 4.160\n",
      "Iter 11/100 - Loss: 3.899\n",
      "Iter 12/100 - Loss: 1.941\n",
      "Iter 13/100 - Loss: 0.344\n",
      "Iter 14/100 - Loss: 0.360\n",
      "Iter 15/100 - Loss: 1.501\n",
      "Iter 16/100 - Loss: 2.298\n",
      "Iter 17/100 - Loss: 1.944\n",
      "Iter 18/100 - Loss: 0.904\n",
      "Iter 19/100 - Loss: 0.177\n",
      "Iter 20/100 - Loss: 0.297\n",
      "Iter 21/100 - Loss: 0.916\n",
      "Iter 22/100 - Loss: 1.281\n",
      "Iter 23/100 - Loss: 1.024\n",
      "Iter 24/100 - Loss: 0.451\n",
      "Iter 25/100 - Loss: 0.111\n",
      "Iter 26/100 - Loss: 0.246\n",
      "Iter 27/100 - Loss: 0.593\n",
      "Iter 28/100 - Loss: 0.733\n",
      "Iter 29/100 - Loss: 0.526\n",
      "Iter 30/100 - Loss: 0.206\n",
      "Iter 31/100 - Loss: 0.087\n",
      "Iter 32/100 - Loss: 0.225\n",
      "Iter 33/100 - Loss: 0.408\n",
      "Iter 34/100 - Loss: 0.413\n",
      "Iter 35/100 - Loss: 0.245\n",
      "Iter 36/100 - Loss: 0.091\n",
      "Iter 37/100 - Loss: 0.096\n",
      "Iter 38/100 - Loss: 0.210\n",
      "Iter 39/100 - Loss: 0.273\n",
      "Iter 40/100 - Loss: 0.210\n",
      "Iter 41/100 - Loss: 0.104\n",
      "Iter 42/100 - Loss: 0.064\n",
      "Iter 43/100 - Loss: 0.117\n",
      "Iter 44/100 - Loss: 0.173\n",
      "Iter 45/100 - Loss: 0.159\n",
      "Iter 46/100 - Loss: 0.093\n",
      "Iter 47/100 - Loss: 0.056\n",
      "Iter 48/100 - Loss: 0.077\n",
      "Iter 49/100 - Loss: 0.115\n",
      "Iter 50/100 - Loss: 0.115\n",
      "Iter 51/100 - Loss: 0.078\n",
      "Iter 52/100 - Loss: 0.050\n",
      "Iter 53/100 - Loss: 0.061\n",
      "Iter 54/100 - Loss: 0.083\n",
      "Iter 55/100 - Loss: 0.086\n",
      "Iter 56/100 - Loss: 0.062\n",
      "Iter 57/100 - Loss: 0.045\n",
      "Iter 58/100 - Loss: 0.053\n",
      "Iter 59/100 - Loss: 0.064\n",
      "Iter 60/100 - Loss: 0.065\n",
      "Iter 61/100 - Loss: 0.050\n",
      "Iter 62/100 - Loss: 0.040\n",
      "Iter 63/100 - Loss: 0.046\n",
      "Iter 64/100 - Loss: 0.052\n",
      "Iter 65/100 - Loss: 0.051\n",
      "Iter 66/100 - Loss: 0.041\n",
      "Iter 67/100 - Loss: 0.037\n",
      "Iter 68/100 - Loss: 0.041\n",
      "Iter 69/100 - Loss: 0.044\n",
      "Iter 70/100 - Loss: 0.042\n",
      "Iter 71/100 - Loss: 0.035\n",
      "Iter 72/100 - Loss: 0.034\n",
      "Iter 73/100 - Loss: 0.036\n",
      "Iter 74/100 - Loss: 0.037\n",
      "Iter 75/100 - Loss: 0.033\n",
      "Iter 76/100 - Loss: 0.030\n",
      "Iter 77/100 - Loss: 0.030\n",
      "Iter 78/100 - Loss: 0.033\n",
      "Iter 79/100 - Loss: 0.031\n",
      "Iter 80/100 - Loss: 0.029\n",
      "Iter 81/100 - Loss: 0.028\n",
      "Iter 82/100 - Loss: 0.028\n",
      "Iter 83/100 - Loss: 0.028\n",
      "Iter 84/100 - Loss: 0.026\n",
      "Iter 85/100 - Loss: 0.025\n",
      "Iter 86/100 - Loss: 0.025\n",
      "Iter 87/100 - Loss: 0.025\n",
      "Iter 88/100 - Loss: 0.025\n",
      "Iter 89/100 - Loss: 0.024\n",
      "Iter 90/100 - Loss: 0.022\n",
      "Iter 91/100 - Loss: 0.022\n",
      "Iter 92/100 - Loss: 0.022\n",
      "Iter 93/100 - Loss: 0.022\n",
      "Iter 94/100 - Loss: 0.020\n",
      "Iter 95/100 - Loss: 0.021\n",
      "Iter 96/100 - Loss: 0.020\n",
      "Iter 97/100 - Loss: 0.019\n",
      "Iter 98/100 - Loss: 0.018\n",
      "Iter 99/100 - Loss: 0.019\n",
      "Iter 100/100 - Loss: 0.017\n",
      "CPU times: user 6.33 s, sys: 9.66 s, total: 16 s\n",
      "Wall time: 2.31 s\n"
     ]
    }
   ],
   "source": [
    "from gpytorch.mlls.variational_elbo import VariationalELBO\n",
    "\n",
    "# Find optimal model hyperparameters\n",
    "model.train()\n",
    "likelihood.train()\n",
    "\n",
    "# Use the adam optimizer\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=0.01)\n",
    "\n",
    "# \"Loss\" for GPs - the marginal log likelihood\n",
    "# n_data refers to the amount of training data\n",
    "mll = VariationalELBO(likelihood, model, num_data=train_y.numel())\n",
    "\n",
    "def train():\n",
    "    num_iter = 100\n",
    "    for i in range(num_iter):\n",
    "        optimizer.zero_grad()\n",
    "        output = model(train_x)\n",
    "        # Calc loss and backprop gradients\n",
    "        loss = -mll(output, train_y)\n",
    "        loss.backward()\n",
    "        print('Iter %d/%d - Loss: %.3f' % (i + 1, num_iter, loss.item()))\n",
    "        optimizer.step()\n",
    "        \n",
    "# Get clock time\n",
    "%time train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f01d03504a8>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQgAAADDCAYAAAB+ro88AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAE4JJREFUeJzt3V9MXNedB/DvgB2GtDFjyCK7GzWdcaQoUqIw+FTdqE+GQYr8sjKL4yja5oWCtLG2VWUlIbIlZNUG7BSpG9ld4TJ+aYQUFWGpD1aiGFLZqrX1XntIXxxV9kzzsDKZxmTAtcMfw+zD3DsMMGfmDvffucz3I1kwc4Y7P+Ze/zj3nHPvL5DNZkFEVEyN1wEQkbqYIIhIigmCiKSYIIhIigmCiKR2WN2AEKJX/3afpmnvFmnvApAB0Kpp2lmr70dE7rHUgxBCxABc0TTtAoCI/riwvRUANE27AiBjPCYif7B6ihEBYCSFpP640BHkeg9GewxE5BuWTjH0noOhFcBHG14SAjBb8LjJyvsRkbssj0EA+VOJW5qm3drqNvr6+rikk8gjQ0NDgWLP25IgAMSKDVAid3rRqH8fAnC/1EZOnjxZ9o3S6TSam5srDtAtqscHMEY7qB4fYD7G/v5+aZvlaU4hRK8xO2EMUgohQnrzR1gbl4gAuGL1/YjIPXbMYpwRQtwVQnxT0DQJAMYph/66jJVTECJyn9VByisAdhd5fn/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hAiIoRoLXaap58CdgBw/XM08Rm2Ijd+BU3Txl0Oz4jB7LEY8aIWrb7/RgDsK9Jm6hiQUbIHUW4FpgorNE3EGANwRT9gIvpjtym9UM3kfnxPTwwRRfdzK9Zm9ZJexOjkYkUlEwTK/1IqHPjlYogUPJfUH7ut7EI1/S+KV7NLJT9D/S/z/wKApmlnPRrkNnOsndG/RhQciLe0WFHVBFHul1JhhWbJGDRNu1DQ3WwFoLkVWIUay7/EMeX24w8BNAkhWj0cIym3n28h13P4ZsPrtgVVE8S2oXc5b3n0l6XkQjWPew9m3S+YLu8q92K3CSFCyH3OgwB+K4TwoqdYiunFisWomiDK/VKWfmmbmI0h5sUApe4jrJ3a5Beq6Qc1kDuv79IHUxs9OH8u9xnex9q5dQa5HoXbysXYC2BQH7zsAaBEEivYx0WPAbNUTRDlDmxLv7RNysUIIUSvMertxSBliYVqxkK28YKZgVCRTTit3Gc4XtAegj4e4bKy+9mgf5aZjc87zcnFisoulNL/qiVRMHUkhLipadp+WbtKMRbcJ2MWub9Ah33QnXedyf08C+CHXvXETMT4jt7e6NWx6BRlEwQReU/VUwwiUgATBBFJMUEQkRQTBBFJMUEQkRQTBBFJMUEQkdT/A/rumGMjR+NRAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Set model and likelihood into eval mode\n",
    "model.eval()\n",
    "likelihood.eval()\n",
    "\n",
    "# Initialize axes\n",
    "f, ax = plt.subplots(1, 1, figsize=(4, 3))\n",
    "\n",
    "with torch.no_grad():\n",
    "    test_x = torch.linspace(0, 1, 101)\n",
    "    predictions = likelihood(model(test_x))\n",
    "\n",
    "ax.plot(train_x.numpy(), train_y.numpy(), 'k*')\n",
    "pred_labels = predictions.mean.ge(0.5).float()\n",
    "ax.plot(test_x.data.numpy(), pred_labels.numpy(), 'b')\n",
    "ax.set_ylim([-1, 2])\n",
    "ax.legend(['Observed Data', 'Mean', 'Confidence'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
